<template>
	<view class="content commonPop">
		<u-popup :show="isAdd" mode="bottom">
			<view class="purchaseBox">
				<view class="baseTop">
					<text>添加楼层</text>
					<view class="closeBox" @click="isAdd = false">
						<image src="../static/closeBtn.png" mode=""></image>
					</view>
				</view>
				
				<u--form labelPosition="top" labelWidth="150rpx" :model="formData" :rules="rules" ref="uForm">
					<!-- <u-form-item label="上级名称:" prop="topName" ref="item1" @click="showType">
						<u--input v-model="formData.topName" readonly suffixIcon="arrow-down"></u--input>
					</u-form-item>
					
					<u-form-item label="地理名称:" prop="companyRoom" ref="item1">
						<u--input placeholder="请输入地理名称,如101,1,2" v-model="formData.companyRoom"></u--input>
					</u-form-item>
					
					<u-form-item label="别名:" prop="otherName" ref="item1">
						<u--input placeholder="请输入别名,如楼,室" v-model="formData.otherName"></u--input>
					</u-form-item> -->
					
					<u-form-item label="楼层信息:" prop="otherName" ref="item1">
						<u--input placeholder="请输入几号楼-几单元-几零几室" v-model="formData.louTxt"></u--input>
					</u-form-item>
				</u--form>		
					
				<view class="submitBtn" @click="submitForm">
					提交
				</view>
				
<!-- 				<u-picker :show="isType" :columns="roomList" keyName="companyRoom" @cancel="isType = false" @confirm="confirmType"></u-picker> -->
				<tki-tree ref="tkitree" @confirm="confirmType" :range="roomList" :rangeKey="rangeKey" :selectParent="true" confirmColor="#4e8af7" />

			</view>	
		</u-popup>
	</view>
</template>

<script>
	import { addCompanyRoom, getFloorList } from '@/api/login.js';
	import tkiTree from "@/components/tki-tree/tki-tree.vue"

	export default {
		name:"addFloor",
		components: {tkiTree},
		data() {
			return {
				isAdd: false,
				isType: false,
				roomList: [],
				rangeKey: 'label',
				formData:{
					companyId: '',
					companyName: '',
					companyRoom: '',
					otherName: '',
					topName: '',
					parentId: 0,
					louTxt: ''
				},
				rules:{
					companyRoom: {
						required: true,
						message: '请填写位置名称',
						trigger: ['blur']
					},
				}
			};
		},
		methods:{
			confirmType(val){
				this.formData.parentId = val[0].id;
				this.formData.topName = val[0].label;
				this.$refs['tkitree']._cancel();
			},
			showType(){
				getFloorList({companyId: uni.getStorageSync('userInfo').companyId}).then(res=>{
					res.data.forEach(item=>{
						item.label = item.companyRoom + item.otherName;
					});
					let treeList = this.listToTreeSimple(res.data);
					this.roomList = treeList;
					this.$refs['tkitree']._show();
				});	
			},
			showInfo(){
				this.formData.companyId = uni.getStorageSync('userInfo').companyId;
				this.formData.companyName = uni.getStorageSync('userInfo').companyName;
				this.formData.companyRoom = '';
				this.formData.otherName = '';
				this.formData.topName = '';
				this.formData.parentId = 0;
				this.isAdd = true;
			},
			showChar(danName){
				let digits = [];
				let letters = [];
				
				for (let char of danName) {
				  if (/\d/.test(char)) { // 如果是数字
				    digits.push(char);
				  } else if (/[\u4e00-\u9fa5]/g.test(char)) { // 如果是字母
				    letters.push(char);
				  }
				}
				if(digits.length == 0 || letters.length == 0) {
					return uni.$u.toast('格式不正确，不包含数字和文字!');
				} else {
					return [digits.join(''),letters.join('')];
				}
				
			},
			// 提交
			submitForm(){
				this.$refs.uForm.validate().then(res => {
					if(this.formData.louTxt == '') {
						return uni.$u.toast('请输入楼层信息!');
					}
					if(String(this.formData.louTxt).includes('-')) {
						if(String(this.formData.louTxt).split('-').length < 4) {
							let danName = String(this.formData.louTxt).split('-')[0];
							const [firstValue, secondValue] = this.showChar(danName);
							let danNameStr = firstValue;
							let danNameOtherStr = secondValue;
							let twoName = String(this.formData.louTxt).split('-')[1];
							let twoNameStr = '';
							let twoNameOtherStr = '';
							// console.log(String(this.formData.louTxt).split('-'))
							// console.log(twoName,'======');
							if(String(this.formData.louTxt).split('-').length == 3) {
								// 三级
								let threeNameStr = '';
								let threeNameOtherStr = '';
								const [firstValueTwo, secondValueTwo] = this.showChar(String(this.formData.louTxt).split('-')[1]);
								const [firstValueThree, secondValueThree] = this.showChar(String(this.formData.louTxt).split('-')[2]);
								twoNameStr = firstValueTwo;
								twoNameOtherStr = secondValueTwo;
								threeNameStr = firstValueThree;
								threeNameOtherStr = secondValueThree;
								// console.log(twoNameStr,twoNameOtherStr);
								// console.log(threeNameStr,threeNameOtherStr);
								this.formData.companyRoom = danNameStr;
								this.formData.otherName = danNameOtherStr;
								addCompanyRoom(this.formData).then(res=>{
									this.formData.companyRoom = twoNameStr;
									this.formData.otherName = twoNameOtherStr;
									this.formData.parentId = res.data;
									addCompanyRoom(this.formData).then(ressAA=>{
										this.formData.companyRoom = threeNameStr;
										this.formData.otherName = threeNameOtherStr;
										this.formData.parentId = ressAA.data;
										addCompanyRoom(this.formData).then(ressBB=>{
											if(ressBB.code == 200) {
												uni.$u.toast('添加成功!');
												this.isAdd = false;
												this.$emit('showForm');
											} else {
												uni.$u.toast(res.msg);
											}
										});	
										
									});	
								});
							} else {
								// 二级
								const [firstValueTwo, secondValueTwo] = this.showChar(twoName);
								twoNameStr = firstValueTwo;
								twoNameOtherStr = secondValueTwo;
								// console.log(twoNameStr,twoNameOtherStr);
								this.formData.companyRoom = danNameStr;
								this.formData.otherName = danNameOtherStr;
								console.log(this.formData,'===1234');
								addCompanyRoom(this.formData).then(res=>{
									this.formData.companyRoom = twoNameStr;
									this.formData.otherName = twoNameOtherStr;
									this.formData.parentId = res.data;
									addCompanyRoom(this.formData).then(ressAA=>{
										if(ressAA.code == 200) {
											uni.$u.toast('添加成功!');
											this.isAdd = false;
											this.$emit('showForm');
										} else {
											uni.$u.toast(ressAA.msg);
										}
									});	
								});	
							}
						} else {
							return uni.$u.toast('只能有两个-!');
						}
						
					} else {
						return uni.$u.toast('格式不正确，请按照提示输入!');
					}
					return;
					if(String(this.formData.louTxt).includes('号楼') && String(this.formData.louTxt).includes('单元')
					&& String(this.formData.louTxt).includes('室')) {
						console.log(this.formData.louTxt);
						
					} else {
						return uni.$u.toast('格式不正确，请按照提示输入!');
					}
					let danName = String(this.formData.louTxt).split('号楼')[1].split('单元')[0];
					let shiName = String(this.formData.louTxt).split('号楼')[1].split('单元')[1].split('室')[0];
					let louName = String(this.formData.louTxt).split('号楼')[0];
					this.formData.companyRoom = louName;
					this.formData.otherName = '号楼';
					// console.log(this.formData,'===1234');
					addCompanyRoom(this.formData).then(res=>{
						console.log(res);
						this.formData.companyRoom = danName;
						this.formData.otherName = '单元';
						this.formData.parentId = res.data;
						addCompanyRoom(this.formData).then(ressAA=>{
							this.formData.companyRoom = shiName;
							this.formData.otherName = '室';
							this.formData.parentId = ressAA.data;
							addCompanyRoom(this.formData).then(ressBB=>{
								if(ressBB.code == 200) {
									uni.$u.toast('添加成功!');
									this.isAdd = false;
									this.$emit('showForm');
								} else {
									uni.$u.toast(res.msg);
								}
							});	
							
						});	
					});	
					return;
					addCompanyRoom(this.formData).then(res=>{
						if(res.code == 200) {
							this.isAdd = false;
							this.$emit('showForm');
						} else {
							uni.$u.toast(res.msg);
						}
					})
				}).catch(errors => {
					return
				});
			},
			listToTreeSimple(data) {
			  const res = [];
			  data.forEach((item) => {
			    const parent = data.find((node) => node.id === item.parentId);
			    if (parent) {
			      parent.children = parent.children || [];
			      parent.children.push(item);
			    } else {
			      // * 根节点
			      res.push(item);
			    }
			  });
			  return res;
			},
		}
	}
</script>

<style scoped lang="scss">
.content{
	.purchaseBox{
		background: #fff;border-radius: 32rpx 32rpx 0px 0px;padding:0 24rpx;
		.baseTop{
			text-align: center;@include flx;justify-content: center;
			text{
				display: block;@include baseFont(36rpx,#333,108rpx);font-weight: 500;
			}
			.closeBox{
				position: absolute;right:38rpx;top:52rpx;
				image{
					display: block;width:24rpx;height:24rpx;
				}
			}
		}
		.submitBtn{
			height: 80rpx;width:100%;@include baseFont(30rpx,#fff,80rpx);font-weight:500;
			background: linear-gradient( 90deg, #588FFF 0%, #3671E9 100%);text-align:center;
			border-radius: 68rpx;margin:32rpx 0 82rpx;
		}
	}
}			
</style>